/*
package com.rykj.energy.task.manage;

import com.rykj.energy.common.dto.TaskCameraRelationDto;
import com.rykj.energy.common.enums.TaskState;
import com.rykj.energy.common.enums.TaskType;
import com.rykj.energy.common.enums.WeeksEnum;
import com.rykj.energy.common.utils.CollectionUtils;
import com.rykj.energy.common.utils.JsonUtil;
import com.rykj.energy.common.utils.StringUtils;
import com.rykj.energy.common.utils.cron.CronUtil;
import com.rykj.energy.common.utils.cron.TaskScheduleModel;
import com.rykj.energy.task.manage.actuator.ITaskManage;
import com.rykj.energy.task.manage.schedule.IQuartzService;
import com.rykj.energy.task.manage.schedule.ManualCaptureJob;
import com.rykj.energy.task.manage.schedule.StartVideoTaskJob;
import com.rykj.energy.task.manage.schedule.StopVideoTaskJob;
import com.rykj.energy.task.manage.vo.CycleTimeDto;
import com.rykj.energy.task.manage.vo.ExecPlanDto;
import com.rykj.energy.task.manage.vo.TriggerDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class TaskManageFactory {

    @Autowired
    private ITaskManage videoTaskManage;

    @Autowired
    private ITaskManage captureTaskManage;

    @Autowired
    private IQuartzService quartzService;

    public boolean startTask(List<TaskCameraRelationDto> voList) throws Exception{

        List<TaskCameraRelationDto> videoList = new ArrayList<>();
        for(TaskCameraRelationDto relationDto : voList){
            //添加执行计划
            String jName = relationDto.getCameraId() + "-" + relationDto.getPreseId()==null?"0":relationDto.getPreseId() + "-" + relationDto.getTaskCode();
            String jGroup = relationDto.getTaskType();
            //{"cycleDays":"Mon,Wed,Sat,Sun","cycleTimes":"20:33:27~20:35:27,23:33:35~23:55:35","cycleType":"everyWeek","time":{“hour”:”1”,"minute":"10","second":"60"}}
            String execPlan = relationDto.getExecPlan();
            if(StringUtils.isBlank(execPlan)){
               if(relationDto.getTaskType().equals(TaskType.video.getCode())){
                   videoList.add(relationDto); //等待开始的视频分析任务
               }else if(relationDto.getTaskType().equals(TaskType.capture.getCode())){
                   throw new Exception("图片分析必须设置执行计划!!");
               }
            } else {
                ExecPlanDto planDto = JsonUtil.stringToBean(execPlan,ExecPlanDto.class);
                TriggerDto triggerDto = createTriggerDto(planDto);//构建触发器参数
                if(relationDto.getTaskType().equals(TaskType.capture.getCode())){

                    quartzService.addTermSecondsJob(ManualCaptureJob.class,jName,jGroup,planDto.getTime().getSecond(),triggerDto,null);
                } else if(relationDto.getTaskType().equals(TaskType.video.getCode())){

                    List<CycleTimeDto> timeDtos = triggerDto.getCycleTimes();
                    Set<Integer> weeks = triggerDto.getWeeks();
                    for(CycleTimeDto timeDto : timeDtos){
                        TaskScheduleModel startModel = new TaskScheduleModel();
                        startModel.setHour(timeDto.getStartHour());
                        startModel.setMinute(timeDto.getStartMinute());
                        startModel.setSecond(timeDto.getStartSeconds());
                        if(weeks!=null&&weeks.size()>0){
                            startModel.setJobType(3);
                            startModel.setDayOfWeeks((Integer[]) weeks.toArray());
                        }else{
                            startModel.setJobType(1);
                        }

                        String startCron = CronUtil.createCronExpression(startModel);
                        quartzService.addJobCron(StartVideoTaskJob.class,jName,jGroup,startCron,null);

                        TaskScheduleModel stopModel = new TaskScheduleModel();
                        stopModel.setHour(timeDto.getStartHour());
                        stopModel.setMinute(timeDto.getStartMinute());
                        stopModel.setSecond(timeDto.getStartSeconds());
                        if(weeks!=null&&weeks.size()>0){
                            stopModel.setJobType(3);
                            stopModel.setDayOfWeeks((Integer[]) weeks.toArray());
                        }else{
                            stopModel.setJobType(1);
                        }

                        String stopCron = CronUtil.createCronExpression(stopModel);
                        quartzService.addJobCron(StopVideoTaskJob.class,jName,jGroup,stopCron,null);
                    }
                }
            }
        }

        if(videoList.size()>0){
            videoTaskManage.startTask(videoList);
        }
        return true;
    }

    private TriggerDto createTriggerDto(ExecPlanDto planDto){
        TriggerDto triggerDto = new TriggerDto();
        String cycleDays = planDto.getCycleDays();
        String cycleTime = planDto.getCycleTimes();
        Set<Integer> w = new HashSet<>();
        if(StringUtils.isNotBlank(cycleDays)){
            String[] weeks = cycleDays.split(",");
            for(String week : weeks){
                w.add(WeeksEnum.getCodeByName(week));
            }
            triggerDto.setWeeks(w);
        }
        if(StringUtils.isNotBlank(cycleTime)) {
            List<CycleTimeDto> cycleTimeDtoList = CollectionUtils.newInstanceList();
            String[] cycleTimes = cycleTime.split(",");
            for (String validity : cycleTimes) {
                CycleTimeDto timeDto = new CycleTimeDto();
                String[] v = validity.split("~");
                String[] startTimes = v[0].split(":");

                if (startTimes.length > 1) {
                    timeDto.setStartHour(Integer.parseInt(startTimes[0]));
                    if (startTimes.length >= 2) {
                        timeDto.setStartMinute(Integer.parseInt(startTimes[1]));
                        if (startTimes.length > 2) {
                            timeDto.setStartSeconds(Integer.parseInt(startTimes[2]));
                        }
                    }
                }
                String[] endTimes = v[1].split(":");
                if (endTimes.length > 1) {
                    timeDto.setEndHour(Integer.parseInt(endTimes[0]));
                    if (endTimes.length >= 2) {
                        timeDto.setEndMinute(Integer.parseInt(endTimes[1]));
                        if (endTimes.length > 2) {
                            timeDto.setEndSeconds(Integer.parseInt(endTimes[2]));
                        }
                    }
                }
                cycleTimeDtoList.add(timeDto);
            }
            triggerDto.setCycleTimes(cycleTimeDtoList);
        }
            return triggerDto;
    }

    public boolean stopTask(List<TaskCameraRelationDto> voList) throws Exception{

        List<TaskCameraRelationDto> videoList = CollectionUtils.newInstanceList();
        List<TaskCameraRelationDto> captureList = CollectionUtils.newInstanceList();

        for(TaskCameraRelationDto relationDto : voList){
            //删除执行计划
            String jName = relationDto.getCameraId() + "-" + relationDto.getPreseId()==null?"0":relationDto.getPreseId() + "-" + relationDto.getTaskCode();
            String jGroup = relationDto.getTaskType();
            quartzService.deleteJob(jName,jGroup);
            if(relationDto.getStatus().equals(TaskState.run)){ //运行中的任务需停止计算
                //停止任务
                if(relationDto.equals(TaskType.video)){
                    videoList.add(relationDto);
                }
                if(relationDto.equals(TaskType.capture)){
                    captureList.add(relationDto);
                }
            }
        }

        if(videoList.size()>0){
            videoTaskManage.stopTask(videoList);
        }
        if(captureList.size()>0){
            captureTaskManage.stopTask(captureList);
        }
        return true;
    }

    public boolean updateTask(List<TaskCameraRelationDto> voList) throws Exception{
        return true;
    }
}
*/
